<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		ignoreHtml,
		errorModal,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		clearEditorData,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	import moment from '@/static/js/moment.js'
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({
				InstCacheKey:"fin-dd-tempPostAdjust-inst-cache",
				busCode:"",
				userListCache:"fin-dd-tempPostAdjust-user-cache",
				userList2Cache:"fin-dd-tempPostAdjust-user2-cache"
			})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			onSelectAdjustWorkDate(value){
				let _that = this
				if(moment(value).isAfter(moment())){
					_that.om.toast('请选择'+moment().format('YYYY-MM-DD')+'或之前的日期')
					_that.$refs.autoform.clear("title3","adjustWorkDate")
				}
				let title  = _that.$refs.autoform.getEditorTitle("title2","nowEntryDate")
				if(_that.$refs.autoform.formdata.nowEntryDate){
					if(moment(value).isBefore(moment(_that.$refs.autoform.formdata.nowEntryDate))){
						_that.om.toast('请选择'+title+'或之后的日期')
						_that.$refs.autoform.clear("title3","adjustWorkDate")
					}
				}
			},
			onSelectAdjustWorkType(item){
				let _that = this
				_that.verifyWorkType(item.code)
			},
			verifyWorkType(code){
				let _that = this
				_that.om.request(
					'tposEmpAdjust/verifyWorkType',
					{
						code: code,
						userId: _that.$refs.autoform.formdata.adjustUserId
					}
				).then(res=>{
					if(res.value){
						confirmModal(_that,'提示信息',res.value,()=>{},()=>{
							_that.$refs.autoform.clear("title3","adjustWorkTypeCode")
							_that.$refs.autoform.clear("title3","adjustWorkDate")
							_that.$refs.autoform.clear("title3","adjustProbation")
							_that.$refs.autoform.clear("title3","adjustEffectWayCode")
							_that.$refs.autoform.clear("title3","adjustSalaryPosId")
						})
					}
				})
			},
			verifyUser1(id){
				let _that = this
				_that.om.request(
					'tempInstAdjustMulti/verifyUser1',
					{
						userId: id
					}
				).then(res=>{
					if(res.value.one){
						errorModal(_that,'提示信息',res.value.one,()=>{
							_that.$refs.autoform.clearGroup('title3')
						})
					}else{
						_that.getUserInfo(id)
						_that.$refs.autoform.clear("title3","adjustWorkTypeCode")
						_that.getWorkType(_that.$refs.autoform.formdata.posId,id)
						_that.getAdjustEffectWay()
						_that.getSalaryPosList(id,_that.$refs.autoform.formdata.posId)
					}
				})
			},
			clearNowUserId(){
				let _that = this
				_that.$refs.autoform.clearGroup("title2")
			},
			posClear(){
				let _that = this
				_that.$refs.autoform.clear("title","posGrade")
				_that.$refs.autoform.clear("title","salaryRange")
				_that.$refs.autoform.clear("title","posId")
				_that.$refs.autoform.clear("title","posCate")
			},
			instClear(){
				let _that = this
				_that.$refs.autoform.clear("title","instId")
				  _that.$refs.autoform.changeEditor("title","posId","data",configSelectData([],'id','posName'))
				_that.$refs.autoform.clear("title","posGrade")
				_that.$refs.autoform.clear("title","salaryRange")
				_that.$refs.autoform.clear("title","posId")
				_that.$refs.autoform.clear("title","posCate")
				_that.$refs.autoform.clear("title","instLevelId")
			},
			getSalaryPosList(userId,posId){
				let _that = this
				_that.om.request(
					'/tposEmpAdjust/getSalaryPosList',
					{
						userId:userId,
						posId:posId
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title3","adjustSalaryPosId","data",configSelectData(res,'id',"posName"))
					_that.$refs.autoform.initSelectIndex("title3","adjustSalaryPosId")
				})
			},
			clearOrgId(){
				let _that = this
				_that.$refs.autoform.clearGroup("title")
				_that.getInstList('')
				_that.$refs.autoform.changeEditor("title","posId","data",configSelectData([],"id","posName"))
			},
			formSubmit(){
				let _that = this
				_that.formdata.busCode = _that.busCode
				if(!_that.formdata.hasOwnProperty("objStr1")){
					let obj = _that.formdata
					_that.formdata.objStr1 = JSON.stringify(obj)
				}
				_that.om.formSave("tposEmpAdjust/addTposEmpAdjust",_that.formdata,_that)
			},
			verifyAdjustUser(){
				let _that = this
				let formdata =_that.$refs.autoform.formdata
				if(!formdata.nowUserId){
					errorModal(_that,"提示信息","请先选择陷入职人员信息中的姓名")
					return false 
				}else{
					return true
				}
			},
			verifyNowUser(){
				let _that = this
				let formdata =_that.$refs.autoform.formdata
				if(!formdata.posId){
					_that.om.toast('请先选择岗位名称',3000)
					return false
				}else{
					return true
				}
			},
			verifyInst(){
				let _that = this 
				let formdata = _that.$refs.autoform.formdata
				if(!formdata.orgShortId){
					_that.om.toast("请先选择组织简称",3000)
					return false
				}else{
					return true
				}
			},
			getUserList(id){
				let _that = this 
					_that.$refs.autoform.changeEditor("title2","nowUserId",{
						link:"../../public/pageSearch/pageSearch",
						"linkParam":{
							other:JSON.stringify({
								posId:id
							}),
							namefield:"nowUserName",
							initload:true,
							url:'tposEmpAdjust/getUserList',
							cachename:_that.userListCache,
							searchfield:"q",
							placeholder:"请输入姓名"
						}
					})
			},
			getAllUser(id){
				let _that = this
					_that.$refs.autoform.changeEditor("title3","adjustUserId",{
						link:"../../public/pageSearch/pageSearch",
						"linkParam":{
							other:JSON.stringify({
								userId:id
							}),
							namefield:"username",
							initload:true,
							url:'tempPosAdjust/getComboboxAll',
							cachename:_that.userList2Cache,
							searchfield:"q",
							placeholder:"请输入姓名"
						}
					})
			},
			onSelectPos(item){
				let _that = this
				let formdata = _that.$refs.autoform.formdata
				formdata.posGrade = item.careercatName
				formdata.posCate = item.fcharacterName
				formdata.salaryRange = item.gradeSalaryRange
				_that.getUserList(item.id)
			},
			getUserInfo(id){
				let _that = this
				_that.om.request(
					"tposEmpAdjust/getUserInfo",
					{
						userId:id
					}
				).then(res=>{
					_that.$refs.autoform.formdata.adjustPosNum = res.value.adjustPosNum
				})
			},
			getWorkType(posId,userId){
				let _that = this
				_that.om.request(
					"tposEmpAdjust/getWorkTypeList",
					{
						posId: posId,
						userId: userId
					}
				).then(res=>{
					if(res.length>1){
						_that.$refs.autoform.changeEditor("title3","adjustWorkTypeCode","data",configSelectData(res,"code","dicname"))
					}else{
						_that.$refs.autoform.changeEditor("title3","adjustWorkTypeCode","data",configSelectData(res,"code","dicname",true))
					}
				})
			},
			getPostList(str){
				let _that = this
				_that.om.request(
					"tposEmpAdjust/getPosList",
					{
						posStr:str
					},
					'post'
				).then(res=>{
					_that.$refs.autoform.changeEditor("title","posId","data",configSelectData(res,"id","posName"))
				})
			},
			onSelectOrgId(item){
				this.getInstList(item.id)
				this.$refs.autoform.clearGroup("title3")
				this.$refs.autoform.clearGroup("title2")
			},
			
			getTipMsg(id){
				let _that = this
				_that.om.request(
					"hrPosUtil/getTipAfterUser",
					{
						zposId:_that.$refs.autoform.formdata.zposId,
						instId:id,
						formCode:_that.busCode
					}
				).then(res=>{
					if(res.value.attributes.pos.length){
						errorModal(_that,'提示信息',res.value.title,function(){
							_that.getPostList(JSON.stringify(res.value.attributes.pos))
						})
					}else{
						errorModal(_that,'提示信息',ignoreHtml(res.value.title))
					}
				})
			},
			getInstList(id){
				let _that = this
				_that.$refs.autoform.changeEditor('title', 'instId',{
					link: "../../public/search/search",
					"linkParam": {
						other: JSON.stringify({
							orgId: id,
							zposId:_that.$refs.autoform.formdata.zposId || ''
						}),
						namefield: 'text',
						searchfield: "q",
						initload: true,
						url: 'tempInstAdjust/findNotAllAndSelf',
						cachename: _that.InstCacheKey,
						placeholder: "请输入部门名称"
					}
				})
			},
			getAdjustEffectWay(){
				let _that = this
				_that.om.request(
					"dictZero/getListByCode",
					{
						code: "35515",
						inCode: "35519"
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title3","adjustEffectWayCode","data",configSelectData(res,"code","dicname"))
				})
			},
			intLoad(options){
				let _that = this
				_that.busCode = options.code
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					{
						url:"temployee/getOrgCombobox"
					},
					{
						url:"wxPosBusiAuth/defaultTableMaker",
						param: {
							code: options.code
						},
						method: "post"
					}
				]
				let fileId = options.fileId
				formModelLoad(ajaxArr,function(res,mainData){
					var defaultPosId = res[2].value.id || '';
					var defaultPosName = res[2].value.posName || '';
					var defaultInstId = res[2].value.instId || '';
					var defaultInstName = res[2].value.instName || '';
					changeEditor("topinfo", "zposId", "data", configSelectData(res[0], "id", "posname"), mainData)
					changeEditor('title',"orgShortId","data",configSelectData(res[1],"id","orgShortName"),mainData)
					changeEditor("title3","adjustUserId",{
						link:"../../public/pageSearch/pageSearch",
						"linkParam":{
							other:JSON.stringify({
								userId:''
							}),
							namefield:"username",
							initload:true,
							url:'tempPosAdjust/getComboboxAll',
							cachename:_that.userList2Cache,
							searchfield:"q",
							placeholder:"请输入姓名"
						}
					},null,mainData)
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.orgShortId){
							_that.$refs.autoform.initSelectVal("title","orgShortId")
						}else{
							_that.$refs.autoform.initSelectIndex("title","orgShortId")
						}
						
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.formdata.zposId = defaultPosId
							_that.$refs.autoform.formdata.zposName = defaultPosName
							_that.$refs.autoform.formdata.zinstId = defaultInstId
							_that.$refs.autoform.formdata.zinstName = defaultInstName
							_that.$nextTick(function() {
								_that.$refs.autoform.initSelectIndex('topinfo',"zposId")
							})
						}else{
							_that.$refs.autoform.initSelectIndex('topinfo',"zposId")
						}
					})
				},options)
			}
		},
		onLoad(options) {
			let _that = this
			_that.intLoad(options)
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.InstCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.instId = d.id
							formdata.instName = d.text
							_that.$refs.autoform.clearGroup("title3")
							_that.$refs.autoform.clearGroup("title2")
							_that.$refs.autoform.clear("title","posId")
							_that.getTipMsg(d.id)
							_that.om.request(
								"tinstitution/getInstInfo",
								{
									instId:d.id
								}
							).then(res=>{
								_that.$refs.autoform.formdata.instLevelId =  res.value.instcatId
								_that.$refs.autoform.formdata.instLevelName =  res.value.instcatName
							})
							uni.removeStorage({
								key:_that.InstCacheKey
							})
						}
					}
				})
				uni.getStorage({
					key:_that.userListCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.nowUserId = d.nowUserId
							formdata.nowUserName = d.nowUserName
							formdata.nowSex = d.nowSex
							formdata.nowEntryDate = d.nowEntryDate
							formdata.nowWorkDate = d.nowWorkDate
							formdata.nowPosNum = d.nowPosNum
							formdata.nowWorkType = d.nowWorkType
							_that.getAllUser(d.nowUserId)
							uni.removeStorage({
								key:_that.userListCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.userList2Cache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.adjustUserId = d.id
							formdata.adjustUserName = d.username
							formdata.adjustSex = d.sexName
							formdata.adjustEntryDate = d.entrydate
							_that.verifyUser1(d.id)
							uni.removeStorage({
								key:_that.userList2Cache
							})
						}
					}
				})
			}
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
